AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "ambient.steam01" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.val2 = 0
	self.val3 = 0
	self.damaged = 0

	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end


function ENT:Use()
end


function quiet_steam(ent)
	ent:StopSound( "ambient.steam01" )
end

function ENT:Output()
	return 1
end


function ENT:Think()
	self:SetOverlayText( "(Suit Dispenser)\nAir: " .. self.val1 .. "\nEnergy: " .. self.val2 .. "\nCoolant: " .. self.val3 )
	if ( self.timer > CurTime() ) then return end
	self.val1 = RD_GetResourceAmount(self, "air")
	self.val2 = RD_GetResourceAmount(self, "energy")
	self.val3 = RD_GetResourceAmount(self, "coolant")
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		local air = RD_GetResourceAmount(self, "air")
		local energy = RD_GetResourceAmount(self, "energy")
		local coolant = RD_GetResourceAmount(self, "coolant")
		local dec_air = 0
		local dec_energy = 0
		local dec_coolant = 0
		
		if ( (2000 - caller.suit.air) < air ) then
			dec_air = (2000 - caller.suit.air)
			air = (air - dec_air)
			caller.suit.air = 2000
		elseif (air > 0) then
			dec_air = air
			caller.suit.air = ( caller.suit.air + air )
			air = 0
		end
		if ( (2000 - caller.suit.energy) < energy ) then
			dec_energy = (2000 - caller.suit.energy)
			energy = (energy - (2000 - caller.suit.energy))
			caller.suit.energy = 2000
		elseif (energy > 0) then
			dec_energy = energy
			caller.suit.energy = ( caller.suit.energy + energy )
			energy = 0
		end
		if ( (2000 - caller.suit.coolant) < coolant ) then
			dec_coolant = (2000 - caller.suit.coolant)
			coolant = (coolant - (2000 - caller.suit.coolant))
			caller.suit.coolant = 2000
		elseif (coolant > 0) then
			dec_coolant = coolant
			caller.suit.coolant = ( caller.suit.coolant + coolant )
			coolant = 0
		end
		RD_ConsumeResource(self, "air", dec_air)
		RD_ConsumeResource(self, "energy", dec_energy)
		RD_ConsumeResource(self, "coolant", dec_coolant)
		caller.Entity:EmitSound( "ambient.steam01" )
		timer.Simple(3, quiet_steam, caller.Entity) 
	end
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
